package com.headline.controller;
//import com.headline.repository.UserRepository;

import com.headline.common.CookieHelper;
import com.headline.model.UserEntity;
import com.headline.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Administrator on 2016/10/20.
 */
@Controller
public class LoginController extends BaseController {
//    @Autowired
//    UserRepository userRepository;


    UserService userService = new UserService();
    private String cookieName = "wzLoginUser";

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "index";
    }


    @ResponseBody
    @RequestMapping(value = "/indexLogin", method = RequestMethod.POST)
    public String indexLogin(UserEntity userEntity) {
        List<UserEntity> list = userService.queryList(" and nickname='" + userEntity.getNickname() + "' and password='" + userEntity.getPassword() + "'");
        if (list != null && list.size() > 0) {
            CookieHelper.addCookie(response, cookieName, userEntity.getNickname() + "|" + list.get(0).getId(), 60 * 24 * 60);
            return "success";
        } else {
            return "error";
        }
    }

    @RequestMapping(value = "/indexLoginOut", method = RequestMethod.GET)
    public String indexLoginOut() {
        CookieHelper.delCookie(request, response, cookieName);
        return "redirect:/";
    }


}